﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer;
using BusinessLayer.cs;

namespace MVCDemo.Controllers
{
    public class Employee2Controller : Controller
    {
        //
        // GET: /Employee2/

        public ActionResult Index()
        {
            EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
            List<Employee> employees = EmployeeBusinessLayer.Employees.ToList();

            return View(employees);
        }

        // It only responds to Get Method from the URL
        [HttpGet]
        [ActionName("Create")]
        public ActionResult Create_Get()
        {
            return View();
        }

        [HttpPost]
        [ActionName("Create")]
        public ActionResult Create_Post()
        {
            if (ModelState.IsValid)
            {
                Employee employee = new Employee();
                TryUpdateModel(employee);

                EmployeeBusinessLayer.AddEmployee(employee);
                return RedirectToAction("Index");
            }

            return View();
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
            Employee employees = EmployeeBusinessLayer.Employees.Single(emp => emp.EmployeeId == id);

            return View(employees);
        }

        [HttpPost]
        [ActionName("Edit")]
        public ActionResult Edit_Post(int id)
        {
            EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
            Employee employee = EmployeeBusinessLayer.Employees.Single(x => x.EmployeeId == id);
            UpdateModel<IEmployee>(employee);
            
            if (ModelState.IsValid)
            {
                EmployeeBusinessLayer.SaveEmployee(employee);
                return RedirectToAction("Index");
            }

            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            EmployeeBusinessLayer.DeleteEmployee(id);
            return RedirectToAction("Index");

        }



    }
}
